package com.lzzy.service.impl;

import com.lzzy.dao.CommentRepository;
import com.lzzy.model.domain.Comment;
import com.lzzy.service.ICommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Service
public class CommentServicelmpl implements ICommentService {

    //注入接口
    @Autowired
    private CommentRepository commentRepository;

    //寻找评论
    @Override
    public List<Comment> findcommentbyArticleid(Integer id){
        List<Comment> comments=commentRepository.findByArticleId(id);
        return comments;
    }

    //添加评论
    @Override
    public void addComment(Integer id, String author, String content){
        //生成一个comment对象
        Comment comment = new Comment();
        //生成一个时间对象
        LocalDate date = LocalDate.now();
        DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        //注入评论时间
        comment.setCreated(date.format(formatter1));
        //注入评论作者
        comment.setAuthor(author);
        //注入评论内容
        comment.setContent(content);
        //注入article_id
        comment.setArticleId(id);

        commentRepository.save(comment);
    }
    //评论管理删除
    public void deleteDiscuss(Integer id) {
        commentRepository.deleteDiscuss(id);
    }

    //删除评论
    public void deleteComments(Integer id){
       commentRepository.deleteDiscuss(id);
    }
}
